var addClass;
require(['monitor', 'highcharts'], function () {
	
	var selectCarId="" ;	
	var _currentBoxNo = 0;//当前电池BMS序号，为0时代表该车不为双路BMS
	var singleLoad = true;
//	var boxNum = 0;;
	
    $('#monitor').monitor({
        canId: '11',
        getOtherCommand:function(){
        	if ($('#bms_no_list').is(':visible')) {//bms radio可以选择时说明是双路BMS，下发多了个参数boxNo
    			if ($('#bms_no1').is(':checked')) {
    				_currentBoxNo = 1;
    			} else {
    				_currentBoxNo = 2;
    			}
    			return 'btryNo:"' + _currentBoxNo + '"';
    		} else {
    			return '';
    		}
        },
        callback: function (data) {
        	loadData(data);
        	if(singleLoad){
        		btryBox(-1);
        		singleLoad = false;
        	}
        },
        clearData: function () {
        	singleLoad = true;
        	$("#parent").empty();
        	loadData({});
        }
    });
    
    
    
	//左侧树
	commonFun.leftTreeNodeClickObj['carChange'] = function(event,
			treeId, treeNode) {
		if($('#monitor').monitor('getStatus')=='monitor'){
			return;
		}
		selectCarId=treeNode.id;
		_currentBoxNo = 0;
		$('#bms_no_list')[treeNode.typeDetail === "CAT00007" ? 'show' : 'hide']();
		
	};
    
	function loadData(data) {
		
		boxNum = data.batteryBoxCount;
		
        $("#batteryClass").setViewData(data, 'id');
        //最高电压箱号
        if((data.highestVoltageBoxNO!=undefined)&&(data.highestVoltageInboxLocation!=undefined)){
            $("#highestVoltageBoxNO").html(data.highestVoltageBoxNO+'/'+data.highestVoltageInboxLocation+'(块)');
        }
        else{
            $("#highestVoltageBoxNO").html("");
        }
        //最低电压箱号
        if((data.lowestVoltageBoxNO!=undefined)&&(data.lowestVoltageInboxLocation!=undefined)){
            $("#lowestVoltageBoxNO").html(data.lowestVoltageBoxNO+'/'+data.lowestVoltageInboxLocation+'(块)');
        }
        else{
            $("#lowestVoltageBoxNO").html("");
        }
        //最高温度箱号
        if((data.highestTemperatureBoxNO!=undefined)&&(data.hightestTemperatureInboxLocation!=undefined)){
            $("#highestTemperatureBoxNO").html(data.highestTemperatureBoxNO+'/'+data.hightestTemperatureInboxLocation+'(块)');
        }
        else{
            $("#lowestVoltageBoxNO").html("");
        }
        //最低温度箱号
        if((data.lowestTemperatureBoxNO!=undefined)&&(data.lowestTemperatureInboxLocation!=undefined)){
            $("#lowestTemperatureBoxNO").html(data.lowestTemperatureBoxNO+'/'+data.lowestTemperatureInboxLocation+'(块)');
        }
        else{
            $("#lowestVoltageBoxNO").html("");
        }
        //电池状态
        var batteryStatus = data.batteryStatus;
        if(batteryStatus!=undefined){
            for (var i = batteryStatus.length - 1; i >= 0; i--) {
                var j = batteryStatus.length - i;
                var signStr = "否";
                var cssStr="green";
                if (batteryStatus[i] == '1') {
                    signStr = "是";
                    cssStr="red";
                }
                $("#batteryStatus" + j).removeClass();
                $("#batteryStatus" + j).addClass(cssStr);
                $("#batteryStatus" + j).html(signStr);
            }
        }
	}
	
	
//	$("#singleMonitor").click(function(){
//		console.info("monitor");
//			btryBox(-1);
//	});
    
    function btryBox(boxNo){
    	singleLoad = true;
    	if(selectCarId == ''){
    		node = commonFun.getLeftTreeSelectedNodes();
    		if(node[0].type == 'CAR'){
    			selectCarId = node[0].id;
    			$.ajaxExt({
    				url : "/deviceParam/getReltivEqmtCd.htm",
    				data : {
    					"carId" : selectCarId
    				}
    			}).done(function(data){
    				devId = data.eqmtCd;
    			});
    		}
    	}
    	if(selectCarId && selectCarId != ''){
    		var msg = "{canId:'14',carId:'"+selectCarId+"',timeSpan:1,interval:0,boxNo:"+boxNo+",btryNo:"+_currentBoxNo+"}";
    		console.info(msg);
    		$.fn.monitor.command(msg,{callback:function(data){
    			console.info(data);
    			//TODO
    			if(data && data.boxList && data.boxList.length > 0){
    				$.each(data.boxList, function(index, item){
    					createBtryBoxChart(item.batteryVoltageList, item.temperatureList, item.boxNo);
    				});
    			}
    		}});
    	}else{
			$.messager.alert("提示", "请至少选择一台车!");
		}
    }
    
    
    function createBtryBoxChart(batteryVoltageList, temperatureList, number){
    	var volArray = batteryVoltageList.split(",");
    	var ar = [];
    	for(var i=0;i<volArray.length;i++){
    		var elem = ['第'+i+"块电压", volArray[i]];
    		ar.push(elem);
    	}
    	
    	var tempratureAr = temperatureList.split(",");
    	var tempratureStr = "";
    	for(var i=0;i<tempratureAr.length;i++){
    		tempratureStr += tempratureAr[i] + ",";
    	}
    	
    	var temprDiv = $('<div>第'+number+'箱监测点温度(℃)：'+tempratureStr.substring(0, tempratureStr.length-1)+'</div>');
    	$('#parent').append(temprDiv);
    	var childdiv = $('<div></div>');
        childdiv.attr('id',''+number);
        $('#parent').append(childdiv);
        childdiv.highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: '单体电池电压'
            },
            credits: {
                enabled: false
            },
            xAxis: {
                type: 'category',
                labels: {
                    //rotation: -45,
                    style: {
                        fontSize: '13px',
                        fontFamily: 'Verdana, sans-serif'
                    }
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: '单体电池电压 (v)'
                }
            },
            legend: {
                enabled: false
            },
            tooltip: {
                pointFormat: '<b>{point.y:.1f} v</b>'
            },
            series: [{
                name: '单体电池电压',
                data: ar
            }]
        });
    }
	
});

